package com.jake.xpage.demos;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.jake.xpage.R;
import com.jake.xpage.page.XPage;
import com.jake.xpage.route.Navigator;
import com.jake.xpage.route.PageRouteDef;

import java.util.ArrayList;
import java.util.List;


public class MainPage extends XPage {
    public MainPage(@NonNull Context context) {
        super(context);
        LayoutInflater.from(getContext()).inflate(R.layout.activity_main, this);
        RecyclerView rvDemo = findViewById(R.id.rv_demos);
        rvDemo.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        DemosAdapter adapter = new DemosAdapter(getDemoList());
        rvDemo.setAdapter(adapter);
    }

    private List<Demo> getDemoList() {
        List<Demo> demos = new ArrayList<>();
        demos.add(new Demo("Apple Music(苹果官方音乐)", PageRouteDef.APPLE_MUSIC));
        demos.add(new Demo("测试", PageRouteDef.TEXT));
        return demos;

    }

    private static class Demo {
        String title;
        String routeUrl;

        public Demo(String title, String routeUrl) {
            this.title = title;
            this.routeUrl = routeUrl;
        }
    }

    private static class DemosAdapter extends RecyclerView.Adapter<DemosAdapter.ViewHolder> {
        @NonNull
        private final List<Demo> mList;

        public DemosAdapter(@NonNull List<Demo> list) {
            this.mList = list;
        }

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_demos, null));
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            holder.bind(mList.get(position));

        }

        @Override
        public int getItemCount() {
            return mList.size();
        }

        private static class ViewHolder extends RecyclerView.ViewHolder {
            private TextView mTextView;
            private Demo mDemo;

            public ViewHolder(@NonNull View itemView) {
                super(itemView);
                RecyclerView.LayoutParams layoutParams = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                itemView.setLayoutParams(layoutParams);
                mTextView = itemView.findViewById(R.id.tv_demo_item);
                mTextView.setOnClickListener(v -> {
                    if (mDemo != null) {
                        Navigator.openUrl(mDemo.routeUrl);
                    }
                });
            }

            public void bind(Demo demo) {
                mDemo = demo;
                if (demo != null) {
                    mTextView.setText(demo.title);
                }
            }
        }
    }
}
